<?php
// 8/18/2009: Immanuel192 create this action
// By the signed contract, only enable high quality option

if (!defined("EXPONENT")) exit("");
include(BASE . "modules/albummodule/core.php");
include(BASE . "subsystems/getid3/getid3.php");
		$getid3 = new getID3();

	$album = null;
	if (isset($_GET['id'])) {
		$album = $db->selectObject("album","id=".$_GET['id']);
		if ($album != null) {
			$loc = unserialize($album->location_data);
			// get album config
			$config=$db->selectObject("albummodule_config","location_data = '".$album->location_data."'");
			if ($config)
			  $max_item=$config->music_per_page;
			else
			   $max_item=15;
		} else {
			echo SITE_404_HTML;
		}
	}
	
	// clicked
	$album->clicked++;
	$db->updateObject($album,"album","id = ".$album->id);
	// end of clicked
	
	exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
	if ($album->image_id == 0) {
		$album->picpath = '';
	} else {
		$file = $db->selectObject('file', 'id='.$album->image_id);
		$album->picpath = $file->directory.'/'.$file->filename;
	}
			
	// for rating
	$rate_1 = floor($album->rate);
	$rate_half = $album->rate - $rate_1;
  $rate_2 = 5- $rate_1 - (($rate_half > 0)?1:0);
  $rate=array();
  for ($j=1;$j<=$rate_1;$j++)
    $rate[$j]=1; // for active rate
  if ($rate_half > 0)
     $rate[$rate_1+1]=3; // for rate half
  for ($j=5-$rate_2+1;$j<=5;$j++)
     $rate[$j]=2; // for active rate
  $album->rate=$rate;
	// end of rating
			
	// for format_type
	$temp=$db->selectObject("album_format","id = ".$album->format);
	$album->format_text = $temp->name;
	// end of format type
	// for genre 
  $temp=$db->selectObject("album_genre","id = ".$album->genre);
  $album->genre_text = $temp->name;
	// end of genre
	// paging
	$count_post = $db->countObjects('musics','album_id='.$album->id);
	
	// cal how many posts
	$page=1;
	if (isset($_GET['page']))
	 $page=intval($_GET['page']);

	$page_count=floor($count_post / $max_item);
	if ($count_post % $max_item != 0)
	 $page_count++;
	if ($page<1 || $page > $page_count) $page=1;
	// end of paging
	// for all musics in this album
	$musics=$db->selectObjects('musics','album_id='.$album->id . " ORDER BY id ASC LIMIT ".($page-1)*$max_item.",{$max_item}");		
			for($j=0; $j<count($musics); $j++) {
			  // fix music info
			  if ($musics[$j]->info == '<p>&#160;</p>')   $musics[$j]->info='';
				$musics[$j]->downloadable=$album->downloadable;
				$musics[$j]->singer_search = htmlspecialchars($musics[$j]->singer);
				if ($musics[$j]->high_quality <= 0) {
				$musics[$j]->high_quality = '';
			} else {			
				$file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
				$musics[$j]->high_quality_id=$musics[$j]->high_quality;
				$musics[$j]->high_quality = $file->directory.'/'.$file->filename;
				if (file_exists($musics[$j]->high_quality))
				{
				 $file_info = $getid3->analyze($musics[$j]->high_quality); 				  
				  //getid3_lib::CopyTagsToComments($file_info); 
				  $musics[$j]->bitrate=round($file_info['bitrate']/1000,0);
				  $musics[$j]->fileformat=$file_info['fileformat'];
				  $file_info=$file_info['playtime_string'];
				  $musics[$j]->file_length = $file_info;
				 }
				}
				/*
				if ($musics[$j]->sound_trackmix == 0) {
				$musics[$j]->sound_trackmix = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sound_trackmix);
				$musics[$j]->sound_trackmix = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->sheet_music == 0) {
				$musics[$j]->sheet_music = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->sheet_music);
				$musics[$j]->sheet_music = $file->directory.'/'.$file->filename;
				}
				
				if ($musics[$j]->demo == 0) {
				$musics[$j]->demo = '';
			} else {
				$file = $db->selectObject('file', 'id='.$musics[$j]->demo);
				$musics[$j]->demo = $file->directory.'/'.$file->filename;
				}
				*/
				// get composer name
				$composer=$db->selectObject('listing',"list_type = 1 AND id = {$musics[$j]->composer_id}");
				$composer_name='';
				if ($composer)
				{
					$composer_name=$composer->name;
				}
				
				// for high_quality_id
				// for tooltip
				//$musics[$j]=musicsmodule::make_info($musics[$j], $composer_name);
			}
			$album->musics=$musics;
	// count how many music
	// fix body of the album
	if ($album->body == '<p>&#160;</p>') $album->body='';
	
    
    // Apr 11, 2011 -trungdt - Fix: load first music item at first
    $first_music_id=-1;
    foreach ($musics as $k=>$v)
    {
        if ($v->high_quality != '')
        {
            $first_music_id=$v->id;
            break;
        }
    }
    // Apr 11, 2011 -trungdt - Fix: load first music item at first
	$album->musics_count =count($musics);
	
	$template = new template("albummodule","_view",$loc);
	$template->register_permissions(array('administrate','configure'),$loc);
	$template->assign('album', $album);
		$template->assign('pages',$page_count);
		$template->assign('page',$page);
	$template->assign('music_no_start',$page *  $max_item);
	// detect IE browser
	global $_SERVER;
  if (isset($_SERVER['HTTP_USER_AGENT']) && 
    (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') >0 ))
    $template->assign('isIE',1);
  else
    $template->assign('isIE',0);
  $file_name=create_playlist($musics);
	$template->assign('plist',$file_name);
    $template->assign('first_music_id',$first_music_id);
	$template->output();
?>
